/**
 * Created by liuzengrong on 2019/8/16.
 */
import React from 'react';
import { connect } from 'dva';
import { message, Button } from 'antd';
import StandardModule from '@/components/hyperspace/modules/StandardModule';

/**
 * 自定义模块
 */
// @ts-ignore
const LocalModule: React.FC = ({ costItems, dispatch }) => (
  <StandardModule
    data={costItems.data}
    config={{
      common: {
        formItemOptions: {
          code: {
            rules: [{ required: true, message: '简称不能为空！' }],
          },
          name: {
            rules: [{ required: true, message: '中文名称不能为空！' }],
          },
          nameEn: {
            rules: [{ required: true, message: '英文名称不能为空！' }],
          },
          type: {
            rules: [{ required: true, message: '费用类型不能为空！' }],
          },
          currency: {
            rules: [{ required: true, message: '币种不能为空！' }],
          },
          unit: {
            rules: [{ required: true, message: '单位不能为空！' }],
          },
        },
      },
      search: {
        fields: ['username', 'mobile', 'email'], // 查询条件
      },
      list: {
        url: '/api/costItems/query',
        fields: ['id', 'code', 'name', 'nameEn', 'type', 'creator', 'creatTime'], // 列表字段
        format: {
          username: {
            render: (value: any) => `格式化 - ${value}`,
          },
        },
        extensions: {
          operations: [
            (record: any) => (
              <a
                onClick={() => {
                  message.info(`扩展操作：${JSON.stringify(record)}`);
                }}
              >
                扩展操作
              </a>
            ),
          ],
        },
      },
      add: {
        url: '/api/costItems/create',
        // width: 680, // 非必填，默认680px
        // title: '新建表单', // 非必填，默认`新建`
        fields: ['code', 'name', 'nameEn', 'type', 'unit', 'currency'], // 新增表单字段
      },
      update: {
        url: '/api/costItems/update',
        // width: 680, // 非必填，默认680px
        // title: '新建表单', // 非必填，默认`新建`
        fields: ['code', 'name', 'nameEn', 'type', 'unit', 'currency'], // 修改表单字段
        primaryKeys: ['id'], // 主键字段列表
      },
      delete: {
        url: '/api/costItems/remove',
        batchDelete: false, // 是否开启批量删除
        fields: ['id'], // 删除依据字段
      },
      extensions: {
        operations: [
          // 外层操作
          (selectedRows: []) => (
            <Button
              style={{ marginLeft: 8 }}
              onClick={() => {
                message.info(`外层扩展操作：${JSON.stringify(selectedRows)}`);
              }}
            >
              批量删除
            </Button>
          ),
        ],
      },
    }}
    namespace="costItems"
    dispatch={dispatch}
  />
);

// @ts-ignore
export default connect(({ costItems }) => ({
  costItems,
}))(LocalModule);
